package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysMsg;
import com.ruoyi.system.service.ISysMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 系统消息Controller
 *
 * @author heyue
 * @date 2022-05-18
 */
@RestController
@RequestMapping("/system/msg")
public class SysMsgController extends BaseController {
    @Autowired
    private ISysMsgService sysMsgService;

    /**
     * 查询系统消息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysMsg sysMsg) {
        startPage();
        List<SysMsg> list = sysMsgService.selectSysMsgList(sysMsg);
        return getDataTable(list);
    }
    /**
     * 我收到的系统消息列表
     */
    @GetMapping("/listByMe")
    public TableDataInfo listByMe(SysMsg sysMsg) {
        startPage();
        sysMsg.setGetUserId(SecurityUtils.getUserId());
        List<SysMsg> list = sysMsgService.selectSysMsgList(sysMsg);
        return getDataTable(list);
    }
    /**
     * 导出系统消息列表
     */
    @Log(title = "系统消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMsg sysMsg) throws IOException {
        List<SysMsg> list = sysMsgService.selectSysMsgList(sysMsg);
        ExcelUtil<SysMsg> util = new ExcelUtil<SysMsg>(SysMsg.class);
        util.exportExcel(response, list, "系统消息数据");
    }

    /**
     * 获取系统消息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        try {
            SysMsg sysMsg = sysMsgService.selectSysMsgById(id);
            return AjaxResult.success(sysMsg);
        } catch (CustomException e) {
            return AjaxResult.error(HttpStatus.MOVED_PERM, e.getMessage());
        }

    }

    /**
     * 新增系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMsg sysMsg) {
        sysMsg.setSendUserId(SecurityUtils.getUserId());
        return toAjax(sysMsgService.insertSysMsg(sysMsg));
    }

    /**
     * 其他模块推送系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping("/pushMessage")
    public AjaxResult pushMessage(String title,String content,Long sendUserId,Long receiveUserId) {
        SysMsg sysMsg=new SysMsg();
        sysMsg.setTitle(title);
        sysMsg.setContent(content);
        sysMsg.setSendUserId(sendUserId);
        sysMsg.setGetUserId(receiveUserId);
        return toAjax(sysMsgService.insertSysMsg(sysMsg));
    }

    /**
     * 修改系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMsg sysMsg) {
        return toAjax(sysMsgService.updateSysMsg(sysMsg));
    }

    /**
     * 删除系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysMsgService.deleteSysMsgByIds(ids));
    }

    /**
     * 已读系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.UPDATE)
    @GetMapping("/read/{ids}")
    public AjaxResult read(@PathVariable String[] ids) {
        return toAjax(sysMsgService.readSysMsgByIds(ids));
    }
}
